class BlackJack {
  // 初始化数据
  constructor() {
    this.cardColor = ["x", "h", "p", "m"];
    this.cardNum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
    this.time = 1;
    this.cardSize = 52;
    this.card = this.initCard();
    this.player = [];
    this.playerCount = 2;
    this.initNum = 2;
  }

  // 抽牌式洗牌法
  shuffle(array) {
    var newArray = [];
    for (let i = array.length - 1; i >= 0; i--) {
      var random = Math.floor(Math.random() * (i + 1));
      newArray.push(array[random]);
      array.splice(random, 1);
    }
    return newArray;
  }

  // 生成纸牌
  generate(color, num) {
    let array = [];
    for (let i = 0; i < this.cardSize * this.time; i++) {
      array.push({
        cardColor: color[i % 4],
        cardNum: num[i % 13],
        imgUrl: "images/" + color[i % 4] + num[i % 13] + ".png",
      });
    }
    return array;
  }

  // 初始化牌盒
  initCard() {
    return this.shuffle(this.generate(this.cardColor, this.cardNum));
  }

  // 初始化玩家并发牌
  initPlayerAndDeal() {
    let tary = this.card;
    let playerCount = this.playerCount;
    let initNum = this.initNum;
    let isBanker = Math.round(Math.random() * playerCount);
    for (let i = 0; i < playerCount; i++) {
      this.player.push(new Player("玩家" + (i + 1)));
    }
    this.player.map((item) => {
      item.porkerList = [];
      item.pointList = [];
    });
    for (let x = 0; x < initNum; x++) {
      for (let y = 0; y < playerCount; y++) {
        this.player[y].isBankerFunc(y == isBanker);
        let porker = tary.pop();
        this.player[y].porkerList.push(porker.imgUrl);
        this.player[y].pointList.push(porker.cardNum);
      }
    }
    for (let m = 0; m < playerCount; m++) {
      this.player[m].computePointSum();
    }
  }
  // 寻找庄家的牌
  findBanker(flag) {
    console.log(flag);
    let play = this.player;
    let banker = play.find((item) => item.isBanker == flag);
    return banker;
  }

  // 添加底牌
  addBack() {
    let banker = this.findBanker(true);
    let bankerFirst = banker.porkerList[0];
    this.player.map((item) => {
      if (item == banker) {
        item.porkerList[0] = "images/back.png";
      }
    });
    return bankerFirst;
  }

  // 打开底牌
  openBack(message) {
    let banker = this.findBanker(true);
    if (banker.porkerList[0] !== message) {
      this.player.map((item) => {
        if (item == banker) {
          item.porkerList[0] = message;
        }
      });
    }
    if (banker.pointSum < 16) {
      this.addCard(true);
      this.openBack(message);
    }
    return;
  }

  // 加牌后是否超过点数
  isBomo(){

  }

  // 加牌
  addCard(flag) {
    let player = this.findBanker(flag);
    console.log("给谁加牌---------->", player);
    let porker = this.card.pop();
    console.log("加的什么牌-------->", porker);
    this.player.map((item) => {
      if (item == player) {
        item.porkerList.push(porker.imgUrl);
        item.pointList.push(porker.cardNum);
        item.computePointSum();
      }
    });
    console.log("加完之后的玩家数据------>", this.player);
  }

  // 比较玩家点数
  comparePlay() {
    let player = this.findBanker(true);
    let players = this.player;
    for (let i = 0; i < players.length; i++) {
      if (
        players[i].pointSum < 21 &&
        players[i].pointSum > player.pointSum &&
        players[i].isBanker == false
      ) {
        players[i].winnerState = "你赢了";
      }

      if (
        players[i].pointSum > 21 &&
        !players[i].isBanker &&
        players[i].isBanker == false
      ) {
        players[i].winnerState = "你爆了";
      }

      if (
        players[i].pointSum <= 21 &&
        players[i].pointSum <= player.pointSum &&
        players[i].isBanker == false
      ) {
        players[i].winnerState = "你输了";
      }
    }
  }
}
class Player {
  constructor(name) {
    this.name = name;
    this.porkerList = [];
    this.pointList = [];
    this.pointSum = 0;
    this.isBanker = false;
    this.winnerState = "";
  }
  computePointSum() {
    let sum = 0;
    let pointNum = this.pointList;
    for (var i = 0; i < pointNum.length; i++) {
      if (pointNum[i] >= 10) {
        sum += 10;
      } else if (pointNum[i] == 1) {
        sum += 11;
      } else {
        sum += pointNum[i];
      }
    }
    if (pointNum.indexOf(1) !== -1 && sum > 21) {
      sum -= 10;
    }
    this.pointSum = sum;
  }
  isBankerFunc(vnum) {
    if (vnum == true) this.isBanker = true;
  }
}
